const App = getApp()
import { MineModel } from '../../../../models/mine.js'
const ModelMine = new MineModel()
Page({
  data: {
    id: '',
    receiver: '',
    phone: '',
    province: '省',
    city: '市',
    area: '区',
    isDefault: false,
    iphoneX: App.globalData.isIphoneX,
    lockAdd: false,
    address:'',
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false,
    navigationBarTitle:''
  },
  onLoad: function(options) {
    this.data.id = options.id
    // 没有传参为新增
    if (this.data.id === undefined) {
      App.WxService.setNavigationBarTitle({
        title: '新增收货地址'
      })
      this.setData({
        navigationBarTitle:'新增收货地址'
      })
    } else {
      App.WxService.setNavigationBarTitle({
        title: '编辑地址'
      })
      this.setData({
        navigationBarTitle:'编辑地址'
      })
      this.initData()
    }
  },
  initData() {
    ModelMine.getAddressDetail({ id: this.data.id }).then(res => {
      this.setData({
        address: res.data.address.address,
        area: res.data.address.area,
        city: res.data.address.city,
        isDefault: res.data.address.isDefault,
        phone: res.data.address.phone,
        province: res.data.address.province,
        receiver: res.data.address.receiver
      })
    })
  },
  // 选为默认地址
  selectedDefault() {
    this.setData({
      isDefault: !this.data.isDefault
    })
  },
  // 微信一件导入
  chooseWXAddress(event) {
    const self = this
    // 一键导入收货地址
    App.WxService
      .chooseAddress()
      .then(res => {
        self.setData({
          province: res.provinceName,
          city: res.cityName,
          area: res.countyName,
          receiver: res.userName,
          phone: res.telNumber,
          address: res.detailInfo
        })
      })
      .catch(err => {
        App.WxService.showToast({ title: '用户已拒绝授权', icon: 'none' })
      })
  },
  // 确定保存地址
  sureSelectAreaListener(e) {
    this.setData({
      show: false,
      province: e.detail.currentTarget.dataset.province,
      city: e.detail.currentTarget.dataset.city,
      area: e.detail.currentTarget.dataset.area
    })
  },
  chooseAddress() {
    var that = this
    that.setData({
      show: true
    })
  },

  bindNameInput:function(e){
    this.setData({
      receiver:e.detail.value
    })
  },

  bindPhoneInput:function(e){
    this.setData({
      phone:e.detail.value
    })
  },

  bindAddressInput:function(e){
    this.setData({
      address:e.detail.value
    })
  },

  submit(e) {
    // const params = e.detail.value
    // 验证
    if (this.data.receiver == '') {
      App.WxService.showToast({ title: '请输入收货人名称', icon: 'none' })
      return false
    }
    if (this.data.phone == '') {
      App.WxService.showToast({ title: '请输入手机或座机', icon: 'none' })
      return false
    }
    var pattern = /(^1[2|3|4|5|6|7|8|9]\d{9}$)|(^\d{3,4}-\d{7,8}$)|(^\d{7,8}$)|(^\d{3,4}-\d{7,8}-\d{1,4}$)|(^\d{7,8}-\d{1,4}$)/ // 手机座机校验
    if (!pattern.test(this.data.phone)) {
      App.WxService.showToast({ title: '请输入正确手机,座机区号用"-"分隔', icon: 'none' ,duration:2000})
      return false
    }
    if (this.data.province == '省') {
      App.WxService.showToast({ title: '请选择省市区', icon: 'none' })
      return false
    }
    if (this.data.address == '') {
      App.WxService.showToast({ title: '请输入详细地址', icon: 'none' })
      return false
    }
    // 如果验证成功
    const formData = {
      receiver: this.data.receiver,
      phone: this.data.phone,
      province: this.data.province,
      city: this.data.city,
      area: this.data.area,
      address: this.data.address,
      isDefault: this.data.isDefault === true ? 1 : 0
    }
    if (this.data.lockAdd) {
      return
    }
    this.data.lockAdd = true
    // 有id编辑
    if (this.data.id) {
      formData.id = this.data.id
      ModelMine.editAddress(formData)
        .then(res => {
          this.data.lockAdd = false
          App.WxService.showToast({ title: '更新成功', icon: 'none' })
          App.WxService.navigateBack({
            delta: 1
          })
        })
        .catch(err => {})
    } else {
      // 新增地址接口
      ModelMine.addAddress(formData)
        .then(res => {
          this.data.lockAdd = false
          App.WxService.showToast({ title: '新增成功', icon: 'none' })
          wx.setStorageSync('addressId', res.data.id)
          App.WxService.navigateBack({
            delta: 1
          })
        })
        .catch(err => {
          this.data.lockAdd = false
        })
    }

    // if (!this.WxValidate.checkForm(e)) {
    //   const error = this.WxValidate.errorList[0]
    //   App.WxService.showModal({
    //     title: '友情提示',
    //     content: `${error.param} : ${error.msg}`,
    //     showCancel: !1
    //   })
    //   return false
    // }

    // // App.HttpService.postAddress(params)
    // this.address.saveAsync(params).then(res => {
    //   const data = res.data
    //   console.log(data)
    //   if (data.meta.code == 0) {
    //     this.showToast(data.meta.message)
    //   }
    // })
  },
  // 验证表单
  verify(params) {
    if (params.receiver == '') {
      App.WxService.showToast({ title: '请输入收货人名称', icon: 'none' })
      return false
    }
    if (params.phone == '') {
      App.WxService.showToast({ title: '请输入手机或座机', icon: 'none' })
      return false
    }
    var pattern = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^\d{3,4}-\d{7,8}$)|(^\d{7,8}$)|(^\d{3,4}-\d{7,8}-\d{1,4}$)|(^\d{7,8}-\d{1,4}$)/ // 手机座机校验
    if (!pattern.test(params.phone)) {
      App.WxService.showToast({ title: '请输入正确手机或座机', icon: 'none' })
      return false
    }
    if (this.data.province == '省') {
      App.WxService.showToast({ title: '请选择省市区', icon: 'none' })
      return false
    }
    if (params.address == '') {
      App.WxService.showToast({ title: '请输入详细地址', icon: 'none' })
      return false
    }
    return true
  }
})
